package com.htandroid.htandroid.webservice;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.htandroid.htandroid.entity.MrqgdDjList;

public class XMLMrqgdlist {

    /**
     * 解析评论xml信息
     *
     * @param result
     * @return
     */
    public static ArrayList<MrqgdDjList> paraseCommentInfors(String result) {
        ArrayList<MrqgdDjList> list = new ArrayList<MrqgdDjList>();
        //字符转换成字节流
        InputStream inputStream = new ByteArrayInputStream(result.getBytes());
        XmlPullParser parser = Xml.newPullParser();
        try {
            parser.setInput(inputStream, "UTF-8");
            int eventType = parser.getEventType();
            MrqgdDjList info = new MrqgdDjList();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
                        break;
                    case XmlPullParser.START_TAG:// 开始元素事件
                        String name = parser.getName();
                        if (name.equalsIgnoreCase("MrqgdDjList")) {
                            info = new MrqgdDjList();
                        } else {
                            eventType = parser.next();
                            if (name.equalsIgnoreCase("Ids")) {
                                info.setIds(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cvouchid")) {
                                info.setCvouchid(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cDepcode")) {
                                info.setcDepcode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cdepname")) {
                                info.setCdepname(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cInvcCode")) {
                                info.setcInvcCode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cInvcName")) {
                                info.setcInvcName(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cInvcode")) {
                                info.setcInvcode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cInvname")) {
                                info.setcInvname(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cInvstd")) {
                                info.setcInvstd(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cInvm_unit")) {
                                info.setcInvm_unit(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iQuantity")) {
                                info.setiQuantity(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iprice")) {
                                info.setIprice(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("iamount")) {
                                info.setIamount(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cvencode")) {
                                info.setCvencode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cvenname")) {
                                info.setCvenname(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cvenemail")) {
                                info.setCvenemail(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cventel")) {
                                info.setCventel(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("dDhDate")) {
                                info.setdDhDate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("binvtype")) {
                                info.setBinvtype(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("djtype")) {
                                info.setDjtype(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("djcvencode")) {
                                info.setDjcvencode(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("djprice")) {
                                info.setDjprice(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("cmemo")) {
                                info.setCmemo(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("canupdjxy")) {
                                info.setCanupdjxy(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("dDate")) {
                                info.setdDate(string2null(parser.getText()));
                            } else if (name.equalsIgnoreCase("bchecked")) {
                                info.setBchecked(string2null(parser.getText()));
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:// 结束元素事件
                        if (parser.getName().equalsIgnoreCase("MrqgdDjList")) {
                            list.add(info);
                            info = null;
                        }
                        break;
                }
                eventType = parser.next();
            }
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    //函数作用是把null 转为空
    public static String string2null(String str) {
        String rtn = "";
        if (str == null || str.equals("null") || str.equals("anyType{}")) {
            rtn = "";
        } else {
            rtn = str;
        }
        return rtn;
    }
}
